using System;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Navigant.Infrastructure.Cases;
using Navigant.Infrastructure.Helpers;
using Navigant.Model.Cases;
using Navigant.Model.PreservationNotices;

namespace Navigant.Web.UI
{
	public class SenderControl : WebControl
	{
		private HtmlTable layoutTable;
		private DropDownList noticeList;
		private TextBox fromTextBox;
		private Button sendButton;
        public event EventHandler SendNotices;
        private ICase currentCase;

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

            try
			{
				this.layoutTable = new HtmlTable();
				this.layoutTable.CellSpacing = 0;
				this.layoutTable.CellPadding = 0;
				this.layoutTable.Border = 0;
				this.Controls.Add(this.layoutTable);

				HtmlTableRow row = new HtmlTableRow();
				layoutTable.Rows.Add(row);

				HtmlTableCell cell;

				cell = new HtmlTableCell();
				cell.InnerText = "Notice:";
				row.Cells.Add(cell);

				SenderControl.AddSpacerCell(row, 5);

				cell = new HtmlTableCell();
				row.Cells.Add(cell);

				this.noticeList = new DropDownList();
				this.noticeList.Enabled = false;
				cell.Controls.Add(this.noticeList);

				SenderControl.AddSpacerCell(row, 10);

				cell = new HtmlTableCell();
				cell.InnerText = "From:";
				row.Cells.Add(cell);

				SenderControl.AddSpacerCell(row, 5);

				cell = new HtmlTableCell();
				row.Cells.Add(cell);

				this.fromTextBox = new TextBox();
				this.fromTextBox.Enabled = false;
				this.fromTextBox.Width = 200;
				this.fromTextBox.Style.Add("font-size", "9pt");
                this.currentCase = CaseService.GetCaseBy(new Uri(SPContext.Current.Web.Url));
                this.fromTextBox.Text = this.currentCase.ClientPortal.DefaultFromEmailAddress;
				cell.Controls.Add(this.fromTextBox);

				SenderControl.AddSpacerCell(row, 10);

				cell = new HtmlTableCell();
				row.Cells.Add(cell);

				this.sendButton = new Button();
				this.sendButton.Enabled = false;
				this.sendButton.Text = "Send Notice(s)";
				this.sendButton.Style.Add("font-size", "8pt");
				this.sendButton.Click += new EventHandler(SendButtonClick);
				cell.Controls.Add(this.sendButton);
			}
			catch (Exception ex)
			{
                Logger.LogError(ex, this.currentCase);
			}
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			
			try
			{
                if (this.noticeList.Items.Count == 0)
                {
                    this.PopulateNotices();
                }
			}
			catch (Exception ex)
			{
				Logger.LogError(ex);
			}
		}

		private static void AddSpacerCell(HtmlTableRow row, int width)
		{
			HtmlTableCell cell = new HtmlTableCell();
			cell.InnerHtml = "&nbsp;";
			cell.Width = width.ToString();
			row.Cells.Add(cell);
		}

		private void PopulateNotices()
		{
			this.noticeList.Items.Clear();

            PreservationNoticeTemplateService service = new PreservationNoticeTemplateService(this.currentCase);
            IList<PreservationNoticeTemplate> templates = service.FindAll();

            foreach (PreservationNoticeTemplate template in templates)
			{
				ListItem listItem = new ListItem();
				listItem.Text = template.Name;
				listItem.Value = template.Id.ToString();
				this.noticeList.Items.Add(listItem);
			}
			
			this.noticeList.Enabled = this.noticeList.Items.Count > 0;
			this.fromTextBox.Enabled = this.noticeList.Items.Count > 0;
			this.sendButton.Enabled = this.noticeList.Items.Count > 0;
		}

        public int NoticeId
        {
            get
            {
                int selectedNoticeId = 0;
                if (!string.IsNullOrEmpty(this.noticeList.SelectedValue))
                {
                    selectedNoticeId = Int32.Parse(this.noticeList.SelectedValue);
                }
                return selectedNoticeId;
            }
        }

        public string FromAddress
        {
            get
            {
                return this.fromTextBox.Text;
            }
        }

        protected void OnSendNotices(object sender, EventArgs e)
        {
            if (this.SendNotices != null)
            {
                this.SendNotices(sender, EventArgs.Empty);
            }
        }

		private void SendButtonClick(object sender, EventArgs e)
		{
			try
			{
                this.OnSendNotices(this, e);
			}
			catch (Exception ex)
			{
				Logger.LogError(ex);
			}
		}
	}
}
